package com.bdqn.tour.dao.impl;

import com.bdqn.tour.dao.BaseDao;
import com.bdqn.tour.dao.GyhQueryALLUserInfoDao;
import com.bdqn.tour.pojo.User;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 查询所有用户信息实现类
 */
public class GyhQueryALLUserInfoDaoImpl extends BaseDao implements GyhQueryALLUserInfoDao {
    /**
     * 查询所有用户信息
     * @return 返回所有用户信息的集合
     */
    @Override
    public List<User> getQueryALLUserInfo() {
        //1.返回对象
        List<User> userList = new ArrayList<>();
        //2.连接对象
        this.conn = this.getConnection();
        //3.编写sql
        String sql = "select id,username,ipone,password,nickname,email,sex,career,qq,status from user";
        try {
            //4.预编译
            this.psmt = this.conn.prepareStatement(sql);
            //5.占位符  设置参数
            //6.执行sql  结果集处理
            this.rs = this.psmt.executeQuery();
            while(rs.next()){
                User user = new User(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getString(7),
                        rs.getString(8),
                        rs.getString(9),
                        rs.getString(10));
                //放放集合
                userList.add(user);

            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            this.closeAll(conn,psmt,rs);
        }

        return userList;
    }

    /**
     * 获取分页数据
     * @return 返回总条数
     */
    @Override
    public Integer getEmpsPageCount() {
        //1.返回对象
        int count = 0;
        //2.打开连接
        this.conn = this.getConnection();

        //3. 获取总数据条数 编写sql
       String sql = "SELECT count(1) cnt FROM user";
        try {
            //4.交给baseDao封装执行，分别进行预编译占位符赋值与执行结果返回
            rs = this.executeQuery(sql);
            //判断结果 如果有数据读取返回true 如果没有数据返回false
            if(rs.next()){
                count = rs.getInt("cnt");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            this.closeAll(conn,psmt,rs);
        }
        //返回总数据条数
        return count;
    }

    @Override
    public List<User> getEmpsByPage(int startIndex, Integer pageSize) {
        //1. 返回对象
        List<User>  userInfoList = new ArrayList<>();
        //2.打开连接
        this.conn = this.getConnection();

        //3.准备SQL,对User表进行分页处理
        String sql = "select id,username,ipone,password,nickname,email,sex,career,qq,status from user limit ?,?";

        try {
            //4.交给baseDao封装执行，分别进行预编译占位符赋值与执行结果返回
            rs = this.executeQuery(sql,startIndex,pageSize);
            //7.解析结果集
            while(rs.next()){
               User user = new User();
               user.setId(rs.getInt(1));
               user.setUsername(rs.getString(2));
               user.setIpone(rs.getString(3));
               user.setPassword(rs.getString(4));
               user.setNickname(rs.getString(5));
               user.setEmail(rs.getString(6));
               user.setSex(rs.getString(7));
               user.setCareer(rs.getString(8));
               user.setQq(rs.getString(9));
               user.setStatus(rs.getString(10));
               userInfoList.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            this.closeAll(conn,psmt,rs);
        }
        return userInfoList;
    }
}
